package models

import (
	"api/utils"
	"time"

	"gorm.io/gorm"
)

// Bottle 瓶子
type Bottle struct {
	Id     uint32 `json:"id"`
	UserId uint32 `json:"userId" gorm:"column:userId;default:0;comment:发布人"`

	Name   string `json:"name" gorm:"size:255;comment:名称" valid:"Required; MaxSize(255);"`
	UseFor uint32 `json:"useFor" gorm:"column:useFor;default:0;comment:适用于，用途"`

	Price uint32 `json:"price" gorm:"default:5;comment:单价" valid:"Required;"`
	Tag   string `json:"tag" gorm:"size:255;comment:标签、特征、类别"`

	Color string `json:"color" gorm:"size:10;comment:颜色" valid:"Required;"`
	Brand string `json:"brand" gorm:"size:20;comment:品牌" valid:"Required; MaxSize(20);"`

	Height uint32 `json:"height" gorm:"default:0;comment:高度" valid:"Required;"`
	Width  uint32 `json:"width"  gorm:"default:0;comment:直径" valid:"Required;"`

	Sequ   uint32 `json:"sequ" gorm:"default:0;comment:显示顺序"`
	Remark string `json:"remark" gorm:"size:255;comment:备注"`

	Images string `json:"images" gorm:"size:255;comment:图集，多个以逗号分隔" valid:"Required; MaxSize(255);"`

	AddTime utils.MyTime `json:"addTime" gorm:"column:addTime;type:datetime;comment:发布时间"`

	Num0 uint32 `json:"num0" gorm:"default:0;comment:库存数量"`
	Num1 uint32 `json:"num1" gorm:"default:0;comment:在售数量"`
	Num2 uint32 `json:"num2" gorm:"default:0;comment:售出数量"`
}

// UploadHistory 上传历史记录
type UploadHistory struct {
	Id       uint32 `json:"id"`
	UserId   uint32 `json:"userId" gorm:"column:userId;default:0;comment:上传者"`
	FilePath string `json:"filePath" gorm:"column:filePath;size:40;comment:本次文件上传后的存储路径，单个文件的存储路径。"`

	AddTime utils.MyTime `json:"addTime" gorm:"column:addTime;type:datetime;comment:文件上传时间"`
}

// Journal 流水帐
type Journal struct {
	Id       uint32 `json:"id"`
	UserId   uint32 `json:"userId" gorm:"column:userId;default:0;comment:操作者"`
	BottleId uint32 `json:"bottleId" gorm:"column:bottleId;default:0;comment:瓶子id"`
	Action   uint32 `json:"action" gorm:"default:0;comment:动作：0 新瓶入库，1 上架，2 售出，3 下架返库，4 丢弃"`
	Num      uint32 `json:"num" gorm:"default:0;comment:数量"`

	AddTime utils.MyTime `json:"addTime" gorm:"column:addTime;type:datetime;comment:操作时间"`
}

func (model *Bottle) BeforeCreate(tx *gorm.DB) error {
	model.AddTime = utils.MyTime{time.Now()} // 设置当前时间为创建时间
	return nil
}
func (model *UploadHistory) BeforeCreate(tx *gorm.DB) error {
	model.AddTime = utils.MyTime{time.Now()}
	return nil
}
func (model *Journal) BeforeCreate(tx *gorm.DB) error {
	model.AddTime = utils.MyTime{time.Now()}
	return nil
}
